package com.edu.service.user;

import com.edu.dao.UserDao;
import com.edu.entity.User;

import java.sql.SQLException;
import java.util.List;

/**
 * @author ZCN
 * @version 1.0
 * @date 2021/11/18 14:18
 */
public class UserServiceImpl implements UserService {
    private UserDao userDao = new UserDao();

    @Override
    public int login(User user) throws SQLException {
        User userfromdb = userDao.login(user.getUsername());
        if(userfromdb == null){
            return -1;
        }
        if(userfromdb.getPassword().equals(user.getPassword())){
            if(userfromdb.getIdent().equals("1")){
                return 1;
            }else {
                return 0;
            }
        }else{
            return -1;
        }
    }

    @Override
    public int register(User user) throws SQLException {
        User userfromdb = userDao.findOne(user.getUsername());
        if(userfromdb != null){
            // 用户不为空，说明用户名重复了
            return -1;
        }
        // 添加普通用户
        userDao.insert(user);
        return 1;
    }

    // 查询所有普通用户
    @Override
    public List<User> selectByordinary() throws SQLException {
        return userDao.selectByOrdinary();
    }

    @Override
    public User selectByUsername(String username) throws SQLException {
        return userDao.login(username);
    }

    @Override
    public User selectByUsername(Integer id) throws SQLException {
        return userDao.findOne(id);
    }
}
